package com.shaoshaohuo.app.ui.ec;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PoiEntity;
import com.shaoshaohuo.app.entity.QuoteResultEntity;
import com.shaoshaohuo.app.entity.post.PurchaseQuote;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.view.DatepickerPopwindow;
import com.shaoshaohuo.app.view.MeasureGridView;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.shaoshaohuo.app.utils.PoiEntityUtils;
import com.shaoshaohuo.app.utils.StringHelper;
import com.shaoshaohuo.app.view.citymenu.CascadingMenuPopWindow;
import com.shaoshaohuo.app.view.citymenu.interfaces.CascadingMenuViewOnSelectListener;
import com.shaoshaohuo.app.view.citymenu.entity.Area;

/**
 * @ClassName ReleaseQuoteActivity
 * @author linmu
 * @Decription 我的报价
 *
 * 	给供货商 报价页面
 */
public class ReleaseQuoteActivity extends BaseActivity implements OnClickListener {
	private static final int SELECT_ADDRESS_POI = 1;
	private TopbarView mTopbarView;

	private MeasureGridView mXingzhiGv;

	private EditText mQuoteEdit;
	private EditText mSupplyNameEdit;
	private EditText mContactNameEdit;
	private EditText mContactPhoneEdit;
	private View mRecivAddressLayout;
	private TextView mRecivAddressText;
	private EditText mDetailAddressText;
	private CascadingMenuPopWindow cascadingMenuPopWindow;
	private String purchaseId;

	private Button mNextButton;
	private PoiEntity startPoi;
	private DatepickerPopwindow datePop;

//	protected int wantTimeYear;
//	protected int wantTimeMonth;
//	protected int wantTimeDay;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_quote);

		initData();
		initView();
		setUpView();
	}

	private void initData() {
		purchaseId = getIntent().getStringExtra(ExtraName.id);
	}

	private void initView() {
		mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

		mXingzhiGv = (MeasureGridView) this.findViewById(R.id.gv_service_list);

		mQuoteEdit = (EditText) this.findViewById(R.id.edittext_quote);
		mSupplyNameEdit = (EditText) this.findViewById(R.id.edittext_supply_name);
		mContactNameEdit = (EditText) this.findViewById(R.id.edittext_contact_name);
		mContactPhoneEdit = (EditText) this.findViewById(R.id.edittext_contact_phone);
		mRecivAddressLayout = this.findViewById(R.id.layout_recev_address);
		mRecivAddressText = (TextView) this.findViewById(R.id.textview_recev_address);
		mDetailAddressText = (EditText) this.findViewById(R.id.textview_recev_address_detail);

		mNextButton = (Button) this.findViewById(R.id.button_next);
	}

	private void setUpView() {
		mTopbarView.setCenterText("我的报价");
		mTopbarView.setLeftView(true, true);

		mRecivAddressLayout.setOnClickListener(this);
		mNextButton.setOnClickListener(this);
		
		// mQuoteEdit.setText("0.00");
		mQuoteEdit.addTextChangedListener(new TextWatcher() {
//			private boolean isChanged = false;
//
//			private String temp = "";

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.toString().contains(".")) {
					if (s.length() - 1 - s.toString().indexOf(".") > 2) {
						s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);
						mQuoteEdit.setText(s);
						mQuoteEdit.setSelection(s.length());
					}
				}
				if (s.toString().trim().substring(0).equals(".")) {
					s = "0" + s;
					mQuoteEdit.setText(s);
					mQuoteEdit.setSelection(2);
				}

				if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
					if (!s.toString().substring(1, 2).equals(".")) {
						mQuoteEdit.setText(s.subSequence(0, 1));
						mQuoteEdit.setSelection(1);
						return;
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}

	@Override
	public void onClick(View v) {
	    DeviceHelper.hideIME(mTopbarView);
		switch (v.getId()) {
		case R.id.button_next:
			releaseQuote();
			break;
		case R.id.layout_recev_address:
			selectAddressPoi();
			break;
		}
	}

	private void selectAddressPoi() {
		
		/*Intent intent1 = new Intent(this, SelectCountrysActivity.class);
		startActivityForResult(intent1, SELECT_ADDRESS_POI);*/
		if (cascadingMenuPopWindow == null) {
			//向三级menu添加地区数据
			cascadingMenuPopWindow = new CascadingMenuPopWindow(this, false);
			cascadingMenuPopWindow
					.setMenuViewOnSelectListener(new NMCascadingMenuViewOnSelectListener());
			cascadingMenuPopWindow.showAsDropDown(mRecivAddressText, 5, 5);
		} else if (cascadingMenuPopWindow != null
				&& cascadingMenuPopWindow.isShowing()) {
			cascadingMenuPopWindow.dismiss();
		} else {
			cascadingMenuPopWindow.showAsDropDown(mRecivAddressText, 5, 5);
		}
	}
	// 级联菜单选择回调接口
	 	class NMCascadingMenuViewOnSelectListener implements
	 			CascadingMenuViewOnSelectListener {

	 		@Override
	 		public void getValue(Area city, String fullname) {
	 			mRecivAddressText.setText(fullname);
	 			startPoi = PoiEntityUtils.convert(city, fullname);
	 		}

	 	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case SELECT_ADDRESS_POI:
				startPoi = (PoiEntity) data.getSerializableExtra(ExtraName.poi);
				mRecivAddressText.setText(startPoi.getTitle());
				break;
			}
		}
	}

	private void releaseQuote() {
		PurchaseQuote purchaseQuote = new PurchaseQuote();

		String price = mQuoteEdit.getText().toString().trim();
		if (TextUtils.isEmpty(price)) {
			showToast("请输入您的报价");
			return;
		}

		String realname = mContactNameEdit.getText().toString().trim();
		String mobile = mContactPhoneEdit.getText().toString().trim();
		if (TextUtils.isEmpty(realname)) {
			showToast("请输入联系人姓名");
			return;
		}
		if (TextUtils.isEmpty(mobile)) {
			showToast("请输入联系人电话");
			return;
		}
		if (!StringHelper.isPhoneNumber(mobile)) {
			showToast("请输入合格的联系人电话");
			return;
		}

		if (startPoi == null) {
			showToast("请选择收货地址");
			return;
		}

		String address = mDetailAddressText.getText().toString().trim();
		if (TextUtils.isEmpty(address)) {
			showToast("请输入详细地址");
			return;
		}


		purchaseQuote.setId(purchaseId);
		purchaseQuote.setPrice(price);
		purchaseQuote.setRealname(realname);
		purchaseQuote.setPhone(mobile);
		purchaseQuote.setOriginaddress(mRecivAddressText.getText().toString() + address);
		purchaseQuote.setSuppliers(mSupplyNameEdit.getText().toString().trim());
		
		
		startLoadingDialog();

		RequestService.getInstance().purchaseQuote(this, purchaseQuote, QuoteResultEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				dismissLoadingDialog();
				if (resultData.isOk()) {
					QuoteResultEntity entity = (QuoteResultEntity) resultData;
					showToast("报价成功");
					// setResult(RESULT_OK);
//					Intent intent = new Intent(ReleaseQuoteActivity.this, SupplyOrderDetailActivity.class);
//					intent.putExtra(ExtraName.orderid, entity.getData().getOrderid());
//					startActivity(intent);
					finish();
				} else {
					showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				dismissLoadingDialog();
				showToast(R.string.please_check_network);
			}
		});
	}
	@Override
	protected void onDestroy() {
		if (cascadingMenuPopWindow != null) {
			cascadingMenuPopWindow.dismiss();
		}
		super.onDestroy();
	}
}
